﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Sob.Common;
using System.Security.Cryptography;

namespace Sob.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //string url = "http://www.hc360.com/";

            //GetWeb web = new GetWeb(url);
            //web.getWebByHomePage();

            //GetWebNewVersion gwnv = new GetWebNewVersion(url,10);
            //gwnv.getWebByHomePage();


            //TestCrawlPage();

            TestBaiduZD();

            Console.Read();
        }



        static void TestCrawlPage()
        {
            string url = "http://www.hc360.com/";
            //CrawlPage crawl = new CrawlPage(url);
            CrawlPage crawl = new CrawlPage();
            if (crawl.DownloadPage(url))
            {
                PageInfo pageInfo = crawl.PageInfo;
                if (pageInfo.IsSuccess)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Charset".PadRight(20, ' ') + pageInfo.Charset);
                    Console.WriteLine("ContentLength".PadRight(20, ' ') + pageInfo.ContentLength);
                    Console.WriteLine("ContentType".PadRight(20, ' ') + pageInfo.ContentType);
                    Console.WriteLine("Title".PadRight(20, ' ') + pageInfo.Title);
                    Console.WriteLine("Keywords".PadRight(20, ' ') + pageInfo.Keywords);
                    Console.WriteLine("Description".PadRight(20, ' ') + pageInfo.Description);
                    Console.WriteLine("ResponseUri".PadRight(20, ' ') + pageInfo.ResponseUri);
                    Console.WriteLine("UseTime".PadRight(20, ' ') + pageInfo.UseTime.Elapsed);
                    Console.WriteLine("");

                    Console.WriteLine("获取内容成功！");
                }
                else
                {
                    Console.WriteLine("读取内容失败！");
                }
            }
            else
            {
                Console.WriteLine("获取内容失败！");
            }
        }

        static void TestRemoveHtmlTags()
        {
            string str = Regex.Replace("AAA\nBBB\nCCC<br>", "^", "开始=>", RegexOptions.Multiline | RegexOptions.IgnoreCase);//多行模式，每行前面加 '开始=>'
            Console.WriteLine(str);
            string s = @"<html><title>title\\标题</title><head><script>alert('JS脚本');</script>head头部</head><body><table><tr><td><!--注释的东西-->TD的内容1</td><td>TD的内容2</td></table><div style='width:100px;'>DIV的内容</div><span>span内容1</spaN><spAN>span内容2</SPAN></body></html>";
            Console.WriteLine(RemoveHTMLTags(s));
        }

        static string RemoveHTMLTags(string html)
        {
            string[] Regexs ={
                        @"<script[^>]*?>.*?</script>",
                        @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
                        @"([\r\n])[\s]+",
                        @"&(quot|#34);",
                        @"&(amp|#38);",
                        @"&(lt|#60);",
                        @"&(gt|#62);",
                        @"&(nbsp|#160);",
                        @"&(iexcl|#161);",
                        @"&(cent|#162);",
                        @"&(pound|#163);",
                        @"&(copy|#169);",
                        @"&#(\d+);",
                        @"-->",
                        @"<!--.*\n"
        };
            string[] Replaces ={
                            "",
                            "",
                            "",
                            "\"",
                            "&",
                            "<",
                            ">",
                            " ",
                            "\xa1", //chr(161),
                            "\xa2", //chr(162),
                            "\xa3", //chr(163),
                            "\xa9", //chr(169),
                            "",
                            "\r\n",
                            ""
        };
            string s = html;
            for (int i = 0; i < Regexs.Length; i++)
            {
                s = new Regex(Regexs[i], RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(s, Replaces[i]);
            }
            s.Replace("<", "");
            s.Replace(">", "");
            s.Replace("\r\n", "");
            return s;
        }

        static void TestBaiduZD()
        {
            //double income = 12345.6789;
            //Console.WriteLine(Math.Round(income, 2));
            //Console.WriteLine(string.Format("{0:.00}", 123456.7891d));
            //double newValue = Math.Round(income, 2);
            //Console.Read();

            StringBuilder randoms = new StringBuilder();
            StringBuilder seeds = new StringBuilder();
            for (int i = 0; i < 100; i++)
            {
                randoms.Append(new Random().Next(0, 100) + " ");
                seeds.Append(new Random(GetRandomSeed()).Next(0, 100) + " ");
            }
            Console.WriteLine(randoms.ToString());
            Console.WriteLine(seeds.ToString());
        }

        /// <summary>
        /// 生成随机因子
        /// </summary>
        /// <returns></returns>
        private static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }

    }
}
